package com.thinvent.nj.ais.service.impl;

import com.thinvent.nj.mybatis.service.impl.BaseCURDServiceImpl;
import com.thinvent.nj.ais.entity.DictGroup;
import com.thinvent.nj.ais.entity.DictItem;
import com.thinvent.nj.ais.mapper.DictGroupMapper;
import com.thinvent.nj.ais.mapper.DictItemMapper;
import com.thinvent.nj.ais.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典Service
 * @author liupj
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class DictServiceImpl extends BaseCURDServiceImpl<DictGroup, String> implements DictService {

    @Autowired
    private DictGroupMapper dictGroupMapper;

    @Autowired
    private DictItemMapper dictItemMapper;



    @Cacheable(cacheNames = "dict", keyGenerator = "keyGenerator")
    @Override
    public List<DictGroup> getAll() {
        return dictGroupMapper.getAll();
    }

    @Cacheable(cacheNames = "dict", keyGenerator = "keyGenerator")
    @Override
    public List<DictItem> getItemsByGroupKey(String groupKey) {
        return dictItemMapper.getItemsByGroupKey(groupKey);
    }

    @Cacheable(cacheNames = "dict", keyGenerator = "keyGenerator")
    @Override
    public List<DictItem> getItemsByGroupId(String groupId) {
        return dictItemMapper.getItemsByGroupId(groupId);
    }

    @Cacheable(cacheNames = "dict", keyGenerator = "keyGenerator")
    @Override
    public DictItem getUniqueItem(String groupKey, String itemKey) {
        return dictItemMapper.getUniqueItem(groupKey, itemKey);
    }

    @Cacheable(cacheNames = "dict", keyGenerator = "keyGenerator")
    @Override
    public DictItem getDictItem(String itemId) {
        return dictItemMapper.get(itemId);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "dict", allEntries = true)
    @Override
    public void insertDictItem(DictItem dictItem) {
        dictItemMapper.insert(dictItem);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "dict", allEntries = true)
    @Override
    public void updateDictItem(DictItem target) {
        dictItemMapper.update(target);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "dict", allEntries = true)
    @Override
    public void deleteDictItem(String id) {
        dictItemMapper.delete(id);
    }

    @Cacheable(cacheNames = "dict", keyGenerator = "keyGenerator")
    @Override
    public DictGroup get(String s) {
        return super.get(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "dict", allEntries = true)
    @Override
    public void delete(String s) {
        super.delete(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "dict", allEntries = true)
    @Override
    public void update(DictGroup dictGroup) {
        super.update(dictGroup);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "dict", allEntries = true)
    @Override
    public void insert(DictGroup dictGroup) {
        super.insert(dictGroup);
    }
}
